package com.lx.yygh.cmn.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lx.yygh.cmn.listener.UploadDataListener;
import com.lx.yygh.cmn.mapper.DictMapper;
import com.lx.yygh.cmn.service.DictService;
import com.lx.yygh.model.cmn.Dict;
import com.lx.yygh.vo.cmn.DictEeVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author c先生
 * @date 2022年10月11日 上午 11:28:19
 * @describe
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {


    @Override
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public List<Dict> findDictEeVo(Long parentId) {
        //查询父节点的所有子节点
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<Dict> list = list(wrapper);
        //查询该子节点有无子节点
        for (Dict dict : list) {
            Integer count = getChirdrenCount(dict.getId());
            if(count>0){
                //大于0说明有子节点
                dict.setHasChildren(true);
            }else {
                //小于0说明没有子节点
                dict.setHasChildren(false);
            }
        }
        return list;
    }

    @Resource
    DictMapper dictMapper;

    //获取父节点的子节点的数量
    @Override
    public Integer getChirdrenCount(Long id) {
        return dictMapper.getChirdrenCount(id);
    }

    //保存数据到数据库中
    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public Integer saveDictEeVo(List<DictEeVo> dictEeVos) {
        return dictMapper.saveDictEeVo(dictEeVos);
    }

    //从本地导入数据，并添加到数据库中
    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void upload(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), DictEeVo.class, new UploadDataListener(this)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    @Override
    public String getName(String dictcode, String value) {
        if("".equals(dictcode)||dictcode==null){
            QueryWrapper<Dict> wrapper = new QueryWrapper();
            wrapper.eq("value",value);
            Dict dict = dictMapper.selectOne(wrapper);
            return dict.getName();
        }else {
            QueryWrapper<Dict> wrapper = new QueryWrapper();
            Dict dict = this.getByDictCode(dictcode);
            wrapper.eq("parent_id",dict.getId()).eq("value",value);
            Dict dict2 = dictMapper.selectOne(wrapper);
            return dict2.getName();
        }
    }

    public Dict getByDictCode(String dictcode){
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.eq("dict_code",dictcode);
        Dict dict = dictMapper.selectOne(wrapper);
        return dict;
    }

    @Override
    public List<Dict> getProvinceList(String dictcode) {
        Dict dict = this.getByDictCode(dictcode);
        Long id = dict.getId();
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.eq("parent_id",id);
        List<Dict> dicts = dictMapper.selectList(wrapper);
        return dicts;
    }

}
